import { Effect, Reducer, Subscription } from 'umi';
import { add_user, get_identitylist, get_user, Put_user, add_identitylist, add_api, get_apilist, set_IdentityApi, get_viewlist, set_IdentityView ,get_apiidentity,get_viewidentity,set_authorityView} from '@/services/usermanage'
import { Iidentity, Iuserinfo, Iapilist, Iviewlist,Iapiidentity,Iidentityview } from '@/interface/usermanage'
export interface userModelIstate {
  identitylist: Iidentity[]
  userallinfo: Iuserinfo[]
  apilist: Iapilist[]
  viewlist: Iviewlist[]
  apiidentity:Iapiidentity[]
  viewidentity:Iidentityview[]
 
}

export interface IndexModelType {
  namespace: 'usermanage';
  state: userModelIstate;
  effects: {
    adduser: Effect
    getidentitylist: Effect
    getuser: Effect
    putuser: Effect
    addidentitylist: Effect
    addapi: Effect
    getapilist: Effect
    setIdentityApi: Effect
    getviewlist: Effect
    setIdentityView: Effect
    getapiidentity:Effect
    getviewidentity:Effect
    setauthorityView:Effect
  };
  reducers: {
    save: Reducer<userModelIstate>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'usermanage',

  state: {
    identitylist: [],  //表明用户身份数据  存放用户的权限
    userallinfo: [],   //用户的信息
    apilist: [],   //api接口数据
    viewlist: [] , //视图权限数据
    apiidentity:[],   //身份和api
    viewidentity:[]   //视图和身份关系  
  },

  effects: {

    //添加用户
    *adduser({ payload }, { call, put }) {
      let result = yield call(add_user, payload)
      return result
    },

    //获取用户身份  用来给用户设置id
    *getidentitylist({ payload }, { call, put }) {
      let result = yield call(get_identitylist)
      if (result.code === 1) {
        yield put({
          type: "save",
          payload: { identitylist: result.data }
        })
      }
    },

    //获取用户信息
    *getuser({ payload }, { call, put }) {
      let result = yield call(get_user)
      if (result.code === 1) {
        yield put({
          type: "save",
          payload: { userallinfo: result.data }
        })
      }
    },

    //更新用户
    *putuser({ payload }, { call, put }) {
      let result = yield call(Put_user, payload)
         return result
    },

    //添加用户身份
    *addidentitylist({ payload }, { call, put }) {
      let result = yield call(add_identitylist, payload)
        return result
    },
    //添加api
    *addapi({ payload }, { call, put }) {
      let result = yield call(add_api, payload)
         return result
    },
   //设置视图接口权限
   *setauthorityView({ payload }, { call, put }){
    let result = yield call(set_authorityView, payload)
         return result
    
   },


    //获取api接口的数据
    *getapilist({ payload }, { call, put }) {
      let result = yield call(get_apilist)
      if (result.code === 1) {
        yield put({
          type: "save",
          payload: { apilist: result.data }
        })
      }
    },

    //给身份设置api接口权限
    *setIdentityApi({ payload }, { call, put }) {
      let result = yield call(set_IdentityApi, payload)
        return result
    },
    //获取视图权限 数据
    *getviewlist({ payload }, { call, put }) {
      let result = yield call(get_viewlist)
      console.log(result.data,"胜多负少的见风使舵拉风")
      if (result.code === 1) {
        yield put({
          type: "save",
          payload: { viewlist: result.data }
        })
      }
    },
    //给身份设置视图权限
    *setIdentityView({ payload }, { call, put }) {
      let result = yield call(set_IdentityView, payload)
        return result
    },

    //身份和api关系  数据
    *getapiidentity({ payload }, { call, put }){
           let result=yield call(get_apiidentity)
            if(result.code===1){
                yield put({
                    type:"save",
                    payload:{apiidentity:result.data}
                })
            }
    },
    //身份和视图关系 数据
    *getviewidentity({ payload }, { call, put }){
      let result=yield call(get_viewidentity)
      if(result.code===1){
        yield put({
            type:"save",
            payload:{viewidentity:result.data}
        })
      }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/main/adduse'||pathname==="/main/showuse") {
          dispatch({
            type: 'getidentitylist',
          })
          dispatch({
            type: "getuser"
          })
          dispatch({
            type: "getapilist"
          })
          dispatch({
            type: "getviewlist"
          })
          dispatch({
            type: "getviewidentity"
          })
          dispatch({
            type: "getapiidentity"
          })
        }
      });
    }
  }
};

export default IndexModel;
